package web.user;

import bean.User;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.Date;

public class UserServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        PrintWriter writer = resp.getWriter();
        try {
            Method declaredMethod = UserServlet.class.getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
            declaredMethod.invoke(this, req, resp);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
            writer.write("login fail");
        }

    }

    public void login(HttpServletRequest req, HttpServletResponse resp) {
        /*System.out.println(req.getParameter("username"));
        System.out.println(req.getParameter("password"));
        System.out.println(req.getParameter("action"));
        */
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User login = userService.login(username, password);
        PrintWriter writer = null;
        try {
            writer = resp.getWriter();
            if (login == null) {
                writer.write("0");
                return;
            }
            writer.write("1");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void register(HttpServletRequest req, HttpServletResponse resp) {
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html,UTF-8");
        String n = "^\\w{4,16}$";
        String e = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String repassword = req.getParameter("repassword");
        String email = req.getParameter("email");
        String telephone = req.getParameter("username");
        String introduction = req.getParameter("introduction");
        String sex = req.getParameter("sex");
        try {
            PrintWriter writer = resp.getWriter();
            if (!username.matches(n)) {
                writer.write("0");
                return;
            }
            if (!password.matches(n)) {
                writer.write("0");
                return;
            }
            if (!password.equals(repassword)) {
                writer.write("0");
                return;
            }
            if (!email.matches(e)) {
                writer.write("0");
                return;
            }
            if ("".equals(sex)) {
                writer.write("0");
                return;
            }
            User user = new User();
            user.setPassword(password);
            user.setUsername(username);
            user.setTelephone(telephone);
            user.setEmail(email);
            user.setGender(sex);
            user.setIntroduce(introduction);
            user.setRegisteTime(new Timestamp(new Date().getTime()));
            int register = userService.register(user);
            if (register > 0) {
                writer.write("1");
            }else
                writer.write("0");
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }
}
